package com.kwan.xframe.mvvm.binding;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

/**
 * @author kwan
 * @date 2023/2/16 14:29
 * @Describe
 */
public class State<T> extends ObservableField<T> {

	private final boolean mIsDebouncing;

	T mValue;

	/**
	 * 务必根据泛型提供初值，以彻底规避 Null 安全问题
	 * Be sure to provide initial values based on generics to completely avoid null security issues
	 *
	 * @param value initial value
	 */
	public State(@NonNull T value) {
		this(value, false);
		mValue = value;
	}

	public State(@NonNull T value, boolean isDebouncing) {
		super(value);
		mIsDebouncing = isDebouncing;
		mValue= value;
	}

	@Override
	public void set(@NonNull T value) {
		boolean isUnChanged = get() == value;
		mValue =value;
		super.set(value);
		if (!mIsDebouncing && isUnChanged) {
			notifyChange();
		}
	}

	@NonNull
	@Override
	public T get() {
		return mValue;
	}
}
